import random
import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['SimHei'] # 用 来 正 常 显 示 中 文 标 签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
# 设置随机种子以确保结果可重现
random.seed(42)

# ============== 数据生成部分 ==============

# 营销员数据
initial_agents = random.randint(500, 1000)  # 期初营销员数量
new_agents = random.randint(100, 300)  # 报告期新增营销员数量
left_agents = random.randint(50, 200)  # 报告期离职营销员数量
active_agents = initial_agents + new_agents - left_agents  # 期末有效营销员数量

# 业务数据
policy_data = {
    "新单数量": random.randint(1000, 5000),
    "新单保费": round(random.uniform(1000000, 10000000), 2),  # 新单原保费收入
    "新单保额": round(random.uniform(10000000, 100000000), 2),  # 新单保险金额
    "续期保费": round(random.uniform(5000000, 50000000), 2),  # 续期业务保费收入
    "短期险保费": round(random.uniform(2000000, 20000000), 2),  # 短期险业务保费收入
    "长期险保费": round(random.uniform(3000000, 30000000), 2),  # 长期险业务保费收入
    "团体业务保费": round(random.uniform(2000000, 30000000), 2),  # 团体业务保费收入
    "个人业务保费": round(random.uniform(3000000, 40000000), 2),  # 个人业务保费收入
}
total_premium = policy_data["新单保费"] + policy_data["续期保费"]  # 总保费收入

# 险种数据 (假设有5种主要险种)
insurance_types = ["人寿保险", "健康保险", "意外伤害保险", "财产保险", "年金保险"]
type_premiums = {
    insurance_type: round(random.uniform(1000000, 8000000), 2)
    for insurance_type in insurance_types
}
type_premiums["总计"] = sum(type_premiums.values()) - type_premiums["总计"] if "总计" in type_premiums else sum(
    type_premiums.values())

# 渠道数据 (假设有4种销售渠道)
channels = ["个人代理", "银行代理", "公司直销", "经纪公司"]
channel_premiums = {
    channel: round(random.uniform(1000000, 7000000), 2)
    for channel in channels
}
channel_premiums["总计"] = sum(channel_premiums.values()) - channel_premiums[
    "总计"] if "总计" in channel_premiums else sum(channel_premiums.values())

# 再保险数据
reinsurance_data = {
    "总分保费收入": round(random.uniform(5000000, 30000000), 2),
    "分出保费": round(random.uniform(1000000, 10000000), 2),
    "分入保费": round(random.uniform(1000000, 10000000), 2),
    "分出保费中最大前3家占比": [round(random.uniform(0.2, 0.5), 2) for _ in range(3)],
    "分入保费中最大前3家占比": [round(random.uniform(0.2, 0.5), 2) for _ in range(3)],
    "非比例合同分保费收入": round(random.uniform(500000, 5000000), 2),
    "比例合同分保费收入": round(random.uniform(500000, 5000000), 2),
    "临时分保分保费收入": round(random.uniform(500000, 5000000), 2),
    "境内分保费收入": round(random.uniform(1000000, 10000000), 2),
    "境外分保费收入": round(random.uniform(1000000, 10000000), 2),
    "非关联交易分保费收入": round(random.uniform(1000000, 10000000), 2),
}
reinsurance_data["合同分保分保费收入"] = reinsurance_data["比例合同分保费收入"] + reinsurance_data[
    "非比例合同分保费收入"]
reinsurance_data["总分保费收入"] = reinsurance_data["分出保费"] + reinsurance_data["分入保费"]

# 假设基期数据 (用于计算增长率和变化率)
base_period_data = {
    "总保费收入": round(
        policy_data["新单保费"] * random.uniform(0.8, 1.2) + policy_data["续期保费"] * random.uniform(0.8, 1.2), 2),
    "险种保费": {
        insurance_type: round(type_premiums[insurance_type] * random.uniform(0.8, 1.2), 2)
        for insurance_type in insurance_types
    },
    "渠道保费": {
        channel: round(channel_premiums[channel] * random.uniform(0.8, 1.2), 2)
        for channel in channels
    },
}
base_period_data["险种保费"]["总计"] = sum(base_period_data["险种保费"].values()) - base_period_data["险种保费"].get(
    "总计", 0)
base_period_data["渠道保费"]["总计"] = sum(base_period_data["渠道保费"].values()) - base_period_data["渠道保费"].get(
    "总计", 0)

# ============== 指标计算部分 ==============

# 1. 营销员相关指标
marketing_indicators = {
    "营销员脱落率(%)": round((left_agents / (initial_agents + new_agents)) * 100, 2),
    "营销员留存率(%)": round(((initial_agents + new_agents - left_agents) / (initial_agents + new_agents)) * 100, 2),
    "人均产能(元/人)": round(total_premium / active_agents, 2),
    "新增营销员占比(%)": round((new_agents / (initial_agents + new_agents)) * 100, 2),
}

# 2. 业务规模与增长指标
business_scale_indicators = {
    "总保费收入(元)": total_premium,
    "保费增长率(%)": round(((total_premium - base_period_data["总保费收入"]) / base_period_data["总保费收入"]) * 100,
                           2),
    "新单保费占比(%)": round((policy_data["新单保费"] / total_premium) * 100, 2),
    "续期保费占比(%)": round((policy_data["续期保费"] / total_premium) * 100, 2),
    "新单保额与保费比": round(policy_data["新单保额"] / policy_data["新单保费"], 2),
    "件均保费(元/件)": round(policy_data["新单保费"] / policy_data["新单数量"], 2),
}

# 3. 业务结构指标
business_structure_indicators = {
    "短期险保费占比(%)": round((policy_data["短期险保费"] / total_premium) * 100, 2),
    "长期险保费占比(%)": round((policy_data["长期险保费"] / total_premium) * 100, 2),
    "团体业务保费占比(%)": round((policy_data["团体业务保费"] / total_premium) * 100, 2),
    "个人业务保费占比(%)": round((policy_data["个人业务保费"] / total_premium) * 100, 2),
}

# 4. 险种分布指标
type_distribution_indicators = {}
for insurance_type in insurance_types:
    type_distribution_indicators[f"{insurance_type}保费占比(%)"] = round(
        (type_premiums[insurance_type] / type_premiums["总计"]) * 100, 2
    )
    type_distribution_indicators[f"{insurance_type}保费增长率(%)"] = round(
        ((type_premiums[insurance_type] - base_period_data["险种保费"][insurance_type]) /
         base_period_data["险种保费"][insurance_type]) * 100, 2
    )

# 5. 渠道分布指标
channel_distribution_indicators = {}
for channel in channels:
    channel_distribution_indicators[f"{channel}保费占比(%)"] = round(
        (channel_premiums[channel] / channel_premiums["总计"]) * 100, 2
    )
    channel_distribution_indicators[f"{channel}保费增长率(%)"] = round(
        ((channel_premiums[channel] - base_period_data["渠道保费"][channel]) /
         base_period_data["渠道保费"][channel]) * 100, 2
    )

# 6. 险种组合变化率
type_shares = [type_premiums[it] / type_premiums["总计"] for it in insurance_types]
base_type_shares = [base_period_data["险种保费"][it] / base_period_data["险种保费"]["总计"] for it in insurance_types]
type_combination_change_rate = round(
    (sum(abs(t - b) for t, b in zip(type_shares, base_type_shares)) / len(insurance_types)) * 100, 2)
business_structure_indicators["险种组合变化率(%)"] = type_combination_change_rate

# 7. 渠道组合变化率
channel_shares = [channel_premiums[c] / channel_premiums["总计"] for c in channels]
base_channel_shares = [base_period_data["渠道保费"][c] / base_period_data["渠道保费"]["总计"] for c in channels]
channel_combination_change_rate = round(
    (sum(abs(c - b) for c, b in zip(channel_shares, base_channel_shares)) / len(channels)) * 100, 2)
business_structure_indicators["渠道组合变化率(%)"] = channel_combination_change_rate

# 8. 再保险业务指标
reinsurance_indicators = {
    "分出保费占比(%)": round((reinsurance_data["分出保费"] / reinsurance_data["总分保费收入"]) * 100, 2),
    "分入保费占比(%)": round((reinsurance_data["分入保费"] / reinsurance_data["总分保费收入"]) * 100, 2),
    "分出保费前3家集中度(%)": round(sum(reinsurance_data["分出保费中最大前3家占比"]) * 100, 2),
    "分入保费前3家集中度(%)": round(sum(reinsurance_data["分入保费中最大前3家占比"]) * 100, 2),
    "非比例合同分保占比(%)": round(
        (reinsurance_data["非比例合同分保费收入"] / reinsurance_data["合同分保分保费收入"]) * 100, 2),
    "比例合同分保占比(%)": round(
        (reinsurance_data["比例合同分保费收入"] / reinsurance_data["合同分保分保费收入"]) * 100, 2),
    "临时分保占比(%)": round((reinsurance_data["临时分保分保费收入"] / reinsurance_data["总分保费收入"]) * 100, 2),
    "境内分保占比(%)": round((reinsurance_data["境内分保费收入"] / reinsurance_data["总分保费收入"]) * 100, 2),
    "境外分保占比(%)": round((reinsurance_data["境外分保费收入"] / reinsurance_data["总分保费收入"]) * 100, 2),
    "非关联交易分保占比(%)": round((reinsurance_data["非关联交易分保费收入"] / reinsurance_data["总分保费收入"]) * 100,
                                   2),
}


# ============== 结果展示部分 ==============

def display_indicators(title, indicators):
    print(f"\n{'=' * 20} {title} {'=' * 20}")
    for key, value in indicators.items():
        print(f"{key}: {value}")


# 显示营销员指标
display_indicators("营销员相关指标", marketing_indicators)

# 显示业务规模与增长指标
display_indicators("业务规模与增长指标", business_scale_indicators)

# 显示业务结构指标
display_indicators("业务结构指标", business_structure_indicators)

# 显示险种分布指标
display_indicators("险种分布指标", type_distribution_indicators)

# 显示渠道分布指标
display_indicators("渠道分布指标", channel_distribution_indicators)

# 显示再保险业务指标
display_indicators("再保险业务指标", reinsurance_indicators)

# 可视化险种保费分布
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.bar(insurance_types, [type_premiums[it] for it in insurance_types])
plt.title("险种保费分布")
plt.xlabel("险种")
plt.ylabel("保费收入(元)")
plt.xticks(rotation=45)

# 可视化渠道保费分布
plt.subplot(1, 2, 2)
plt.bar(channels, [channel_premiums[c] for c in channels])
plt.title("渠道保费分布")
plt.xlabel("渠道")
plt.ylabel("保费收入(元)")
plt.xticks(rotation=45)

plt.tight_layout()
plt.savefig("insurance_indicators.png")
plt.show()
